<SDOCTYPv HTM.>
<html>
<!--
Bug 1060093 - Test DebuggerServer.getProcess
-->
<head>
  <meta charset="utf-8">
  <title>Mozilla Bug</title>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
</head>
<body>
<pre id="test">
<script type="application/javascript;version=1.8">

let Cu = Components.utils;
let Cc = Components.classes;
let Ci = Components.interfaces;

let {require} = Cu.import("resource://devtools/shared/Loader.jsm", {});
let {DebuggerClient} = require("devtools/shared/client/main");
let {DebuggerServer} = require("devtools/server/main");

window.onload = function() {
  SimpleTest.waitForExplicitFinish();

  SpecialPowers.pushPrefEnv({
    "set": [
      // Always log packets when running tests.
      ["devtools.debugger.log", true],
      // Enabled mozbrowser frame to support remote=true
      ["dom.mozBrowserFramesEnabled", true],
      // Allows creating a branch new process when creation the iframe
      ["dom.ipc.processCount", 10],
    ]
  }, runTests);
}

function runTests() {
  // Instantiate a minimal server
  if (!DebuggerServer.initialized) {
    DebuggerServer.init();
  }
  DebuggerServer.allowChromeProcess = true;
  if (!DebuggerServer.createRootActor) {
    DebuggerServer.addBrowserActors();
  }

  let client, iframe, processCount;

  function connect() {
    // Fake a first connection to the content process
    let transport = DebuggerServer.connectPipe();
    client = new DebuggerClient(transport);
    client.connect().then(listProcess);
  }

  function listProcess() {
    // Call listProcesses in order to start receiving new process notifications
    client.addListener("processListChanged", function listener() {
      client.removeListener("processListChanged", listener);
      ok(true, "Received processListChanged event");
      getProcess();
    });
    client.mainRoot.listProcesses(response => {
      processCount = response.processes.length;
      // Create a remote iframe to spawn a new process
      createRemoteIframe();
    });
  }

  function createRemoteIframe() {
    iframe = document.createElement("iframe");
    iframe.mozbrowser = true;
    iframe.setAttribute("remote", "true");
    iframe.setAttribute("src", "data:text/html,foo");
    document.body.appendChild(iframe);
  }

  function getProcess() {
    client.mainRoot.listProcesses(response => {
      ok(response.processes.length >= 2, "Got at least the parent process and one child");
      is(response.processes.length, processCount+1 , "Got one additional process on the second call to listProcesses");

      // Connect to the first content processe available
      let content = response.processes.filter(p => (!p.parent))[0];

      client.getProcess(content.id).then(response => {
        let actor = response.form;
        ok(actor.consoleActor, "Got the console actor");
        ok(actor.chromeDebugger, "Got the thread actor");

        // Ensure sending at least one request to an actor...
        client.request({
          to: actor.consoleActor,
          type: "evaluateJS",
          text: "var a = 42; a"
        }, function (response) {
          ok(response.result, 42, "console.eval worked");
          cleanup();
        });
      });
    });
  }

  function cleanup() {
    client.close().then(function () {
      DebuggerServer.destroy();
      iframe.remove();
      SimpleTest.finish()
    });
  }

  connect();
}

</script>
</pre>
</body>
</html>
